﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace SinacorRules
{
    public sealed class Rule_BovespaDoNotUseStaticFieldsAtViewPresenter : BaseAddIn.Rules.BaseRuleAddIn
    {
        private Dictionary<string, bool> _isViewPresenter;

        public Rule_BovespaDoNotUseStaticFieldsAtViewPresenter()
            : base("BovespaDoNotUseStaticFieldsAtViewPresenter",
                   "SinacorRules.SinacorRules",
                   typeof(Rule_BovespaDoNotUseStaticFieldsAtViewPresenter).Assembly)
        {
        }

        protected sealed override void Before_IniciaAnalise()
        {
            _isViewPresenter = new Dictionary<string, bool>();
        }

        protected sealed override void Check(CodeProperty propriedade, CodeNamespace namespaceElement, CodeClass2 classe)
        {
            EditPoint texto;
            string declaracao;

            if (!_isViewPresenter.ContainsKey(classe.FullName))
                _isViewPresenter.Add(classe.FullName,
                                     (Common.Rotinas.CheckClassInheritance(classe, typeof(Sinacor.Infra.UI.Controls.SmartClient.SinacorWindow))
                                      || Common.Rotinas.CheckClassInheritance(classe, typeof(Sinacor.Infra.UI.Common.BasePresenter<Sinacor.Infra.UI.Common.IBaseView>))));


            if (_isViewPresenter[classe.FullName])
            {
                texto = propriedade.StartPoint.CreateEditPoint();
                declaracao = texto.GetText(propriedade.EndPoint);

                if (declaracao.Contains("static"))
                {
                    GeraProblemaAddIn(propriedade.StartPoint.Parent.Parent.FullName,
                                      propriedade.StartPoint.Line,
                                      propriedade.StartPoint.DisplayColumn,
                                      "DoNotUseStaticPropertiesAtViewPresenterResolution",
                                      classe.Name,
                                      propriedade.Name,
                                      namespaceElement.Name);
                }
            }
        }

        protected sealed override void Check(CodeVariable variavel, CodeNamespace namespaceElement, CodeClass2 classe)
        {
            EditPoint texto;
            string declaracao;

            if (!_isViewPresenter.ContainsKey(classe.Name))
                _isViewPresenter.Add(classe.Name,
                                     (Common.Rotinas.CheckClassInheritance(classe, typeof(Sinacor.Infra.UI.Controls.SmartClient.SinacorWindow))
                                      || Common.Rotinas.CheckClassInheritance(classe, typeof(Sinacor.Infra.UI.Common.BasePresenter<Sinacor.Infra.UI.Common.IBaseView>))));

            if (_isViewPresenter[classe.Name])
            {
                texto = variavel.StartPoint.CreateEditPoint();
                declaracao = texto.GetText(variavel.EndPoint);

                if (declaracao.Contains("static"))
                {
                    GeraProblemaAddIn(variavel.StartPoint.Parent.Parent.FullName,
                                      variavel.StartPoint.Line,
                                      variavel.StartPoint.DisplayColumn,
                                      "DoNotUseStaticFieldsAtViewPresenterResolution",
                                      classe.Name,
                                      variavel.Name,
                                      namespaceElement.Name);
                }
            }
        }
    }
}
